package com.ruoyi.web.controller.tool;

import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.sign.SignUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.ReportInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/tool/httpUtil")
public class HttpUtil {

    @Value("${appId}")
    private String appId;

    @Value("${appSecret}")
    private String appSecret;

    @Value("${parkId}")
    private String parkId;

    @Value("${parkService}")
    private String parkService;

    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 获取参数配置列表
     */
    @PostMapping("/clientUrl")
    public AjaxResult clientUrl(@RequestParam Map<String,Object> params)
    {
        this.setParams(params);
        return AjaxResult.success(getUrlData(params));
    }

    public Object getUrlData(Map<String,Object> params){
        String res = HttpRequest.post(parkService+params.get("url"))
                .header(Header.CONTENT_TYPE,"application/json;charset=utf-8")
                .header("version","1.0.0")
                .body(JSONObject.toJSON(params).toString())
                .execute().body();
        return JSONObject.parse(res);
    }

    public Map<String,Object> setParams(Map<String,Object> params){
        params.put("appId",appId);
        params.put("parkId",parkId);
        params.put("ts",new Date().getTime());
        params.put("reqId", IdUtils.simpleUUID());
        params.put("key",SignUtils.paramsSign(params,appSecret));
        return params;
    }

    //车位预约
    public AjaxResult reserveSpace(ReportInfo info)
    {
        Map<String,Object> params = new HashMap<>();
        params.put("url","/api/find/ReserveSpace");
        params.put("serviceCode","reserveSpace");
        params.put("plateNo",info.getCarNumber());
        params.put("type",info.getReserveType());
        params.put("addrId",info.getReserveId());
        params.put("enterTime",format.format(info.getComeTime()));
        params.put("leaveTime",format.format(info.getOutTime()));
        params.put("failureTime",format.format(info.getOutTime()));
        params.put("flag","1");
        params.put("cardType","100");
        this.setParams(params);
        return AjaxResult.success(getUrlData(params));
    }

    //取消预约
    public AjaxResult cancelReserveSpace(ReportInfo info)
    {
        Map<String,Object> params = new HashMap<>();
        params.put("url","/api/find/CancelReserveSpace");
        params.put("serviceCode","cancelReserveSpace");
        params.put("orderNo",info.getOrderNo());
        this.setParams(params);
        return AjaxResult.success(getUrlData(params));
    }

    //修改预约时间
    public AjaxResult changeReserve(ReportInfo info)
    {
        Map<String,Object> params = new HashMap<>();
        params.put("url","/api/find/ReserveSpace_ChangeTime");
        params.put("serviceCode","reserveSpace_ChangeTime");
        params.put("orderNo",info.getOrderNo());
        params.put("enterTime",format.format(info.getComeTime()));
        params.put("leaveTime",format.format(info.getOutTime()));
        params.put("failureTime",format.format(info.getOutTime()));
        this.setParams(params);
        return AjaxResult.success(getUrlData(params));
    }


}
